﻿package stfei.HorizList {
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;

	import stfei.HorizList.core.HorizListEvent
	import stfei.HorizList.core.InavBar;

	/**
	 * ...
	 * @author Stfei设计 as3
	 项目名称：
	 类应用：导航条实现接口R11
	 */
	public class Navigation extends MovieClip implements InavBar {
		private var prevBn:MovieClip
		private var nextBn:MovieClip
		private var navBarText:TextField
		private var _totalLen:int
		private var _numPage:int
		private var _page:int
		private var totalPages:int

		///总个数
		public function set totalLen(value:int):void {
			_totalLen = value;
		}

		///每页个数
		public function set numPage(value:int):void {
			_numPage = value;
		}

		///获取当前页
		public function get page():int {
			return _page
		}

		public function set page(value:int):void
		{
			_page = value;
		}

		//创建导航条，参数：传入导航条影片
		public function Navigation(navBar:MovieClip){
			_page = 1
			prevBn = navBar.prev
			nextBn = navBar.next
			navBarText = navBar.navText
			if (nextBn){
				nextBn.buttonMode = true
				nextBn.addEventListener(MouseEvent.CLICK, nextClick)

			} else {
				throw new Error("导航条缺少next影片");
			}
			if (prevBn){
				prevBn.buttonMode = true
				prevBn.addEventListener(MouseEvent.CLICK, prevClick)
			} else {
				throw new Error("导航条缺少prev影片");
			}
		}

		public function setNavBar():void {
			totalPages = Math.ceil(_totalLen / _numPage)
			if(navBarText){
				navBarText.text = _page + "/" + totalPages + "页 共" + _totalLen + "条记录";
			}
		}

		private function prevClick(e:MouseEvent):void {
			if ((_page - 1) > 0){
				_page--;
				dispatchEvent(new Event(HorizListEvent.CHANGEPAGE_START_PREV))
				this.CallEvent("prev")
			}

		}

		private function nextClick(e:MouseEvent):void {
			if ((_page + 1) <= totalPages){
				_page++;
				dispatchEvent(new Event(HorizListEvent.CHANGEPAGE_START_NEXT))
				this.CallEvent("next")
			}
		}

		/* INTERFACE stfei.HorizList.core.InavBar */

		public function CallEvent(Direction:String):void
		{
			if(navBarText){
				navBarText.text = _page + "/" + totalPages + "页 共" + _totalLen + "条记录"
			}
		}
	}

}

